package com.atguigu.educenter.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.orderVo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.LoginVo;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.IUcenterMemberService;
import com.atguigu.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author AIR
 * @since 2022-10-07
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private IUcenterMemberService iUcenterMemberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        // LoginVo 对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = iUcenterMemberService.login(loginVo);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        iUcenterMemberService.registerUser(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = iUcenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据用户id获取用户信息
    @GetMapping("getUserInfoOrder/{userId}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String userId){
        UcenterMember member = iUcenterMemberService.getById(userId);
        //把member对象里面的值赋值给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }


}
